var orderDAO = require('../models/orderDAO')
var shopDAO = require('../models/shopDAO')
var orderController = {
    //查询订单
    getOrderList: function (req, res) {
        var orderId = req.params.orderId
        orderDAO.getOrderDetail(orderId, function (err, results) {
            console.log(results)
            if (err) {
                res.json({ code: 500,data:-1, msg: '订单列表获取失败！'})
            } else {
                res.json({ code: 200, data: results, msg: '订单列表获取成功' })
            }
        })
    },
    //查询订单详情
    getOrderDetail: function (req, res) {
        var userId = req.query.userId
        console.log('订单里用户Id：'+userId)
        orderDAO.getOrderList(userId,function(err,results){
            if (err) {
                res.json({ code: 500,data:-1, msg: '订单列表获取失败！'})
            } else {
            //   console.log(results[0].orderId)
              var orderIds=[]
              for(var i=0;i<results.length;i++){
                  orderIds.push(results[i].orderId)
              }
              orderIds.push(0)
              orderDAO.getOrderDetail(orderIds, function (err, results) {
                    if (err) {
                        res.json({ code: 500,data:-1, msg: '订单详情列表获取失败' })
                    } else {
                        
                        res.json({ code: 200, data: results, msg: '订单详情列表获取成功' })
                    }
                })
            }
        })
      

    },
    //删除订单
    delOrder: function (req, res) {
        var orderId = req.query.orderId
        console.log('orderId='+orderId)
        //先删详情表
        orderDAO.delOrderDetail(orderId, function (err, results) {
            if (err) {
                res.json({ code: 500,data:-1, msg: '删除订单失败！' })
            } else {
                if (results.affectedRows == 0) {
                    res.json({ code: 200,data:-1, msg: '删除订单失败！' })
                } else {
                    // 再删订单表
                    orderDAO.delOrder(orderId, function (err, results) {
                        if (err) {
                            res.json({ code: 500,data:-1, msg: '删除订单失败！' })
                        } else {
                            if (results.affectedRows == 0) {
                                res.json({ code: 200,data:-1, msg: '删除订单失败！' })
                            } else {
                                res.json({ code: 200, data: results, msg: '删除订单成功' })
                            }
                        }
                    })
                }
            }
        })
    },
    
    //下订单
    addOrder: function (req, res) {
        var userId = req.params.uId
        console.log(userId)
        console.log(req.body.shopIds)
        var shopIds = req.body.shopIds
        shopIds.push(0);
        // 添加订单表
        console.log(shopIds)
        orderDAO.addOrder(userId, function (err, results) {
            if (err) {
                console.log('订单表：'+err)
                res.json({ code: 500,data:-1, msg: '订单失败' })
            } else {
                if (results.affectedRows == 0) {
                    res.json({ code: 200,data:-1, msg: '订单失败！' })
                } else {
                    //添加订单详情
                    var ids = { shopIds, orderId: results.insertId }
                    orderDAO.addOrderDetail(ids, function (err, results) {
                        if (err) {
                            console.log("详情表"+err)
                            res.json({ code: 500,data:-1, msg: '订单详情失败' + err.message })
                        } else {
                            if (results.affectedRows == 0) {
                                res.json({ code: 200,data:-1, msg: '订单详情失败！' })
                            } else {
                                // res.json({ code: 200, data: results, msg: '订单详情生成成功' })
                                //删除购物车
                                shopDAO.delProduct(shopIds, function (err, results) {
                                    if (err) {
                                        console.log(err)
                                        res.json({ cede: 500,data:-1, msg: "删除购物车失败" })
                                    } else {
                                        if (results.affectedRows == 0) {
                                            res.json({ cede: 200,data:-1, msg: "删除购物车失败" })
                                        } else {
                                            res.json({ code: 200, data:ids.orderId, msg: "删除购物车成功" })
                                        }
                                    }
                                })
                            }
                        }
                    })
                }
            }
        })
    }
}
module.exports = orderController;
